package se_sspslot

import (
	"fmt"
	"wx-api/ssp-api/cache"
	"wx-api/ssp-api/errmsg"
	"wx-api/ssp-api/handlers"
	"wx-api/ssp-api/model/m_sspslot"
	"wx-api/ssp-api/services/comm"
	"wx-api/ssp-api/utils"
)

func List(c *handlers.Controller) {
	id := c.GetString("id")
	name := c.GetString("name")
	flag := c.GetInt32("flag", 0)
	status := c.GetInt32("status", 1)
	pageNum, pageSize := c.GetPage()
	ssp_uid := comm.GetUserInfo(c)
	if ssp_uid == "" {
		return
	}
	data, err := m_sspslot.List(id, name, ssp_uid, flag, status, pageNum, pageSize)
	if err != nil {
		c.SetErrorMsg(nil, errmsg.GetError("select list is error"))
		return
	}
	tmplist := []m_sspslot.SspSlot{}
	ttt := data.Data.(*[]m_sspslot.SspSlot)
	for _, v := range *ttt {
		tmp := v
		sinfo, ok := cache.CacheSspAppMapNGet(v.Mid)
		if ok {
			tmp.MidName = sinfo.Name
		}
		tinfo, ok := cache.CacheSspTemplateMapNGet(v.TemplateId)
		if ok {
			tmp.TemplateName = tinfo.CreateTemplateId
		}
		tmplist = append(tmplist, tmp)
	}
	data.Data = tmplist
	c.SetErrorMsg(data, errmsg.SuccessMsg())
	return
}

func Add(c *handlers.Controller) {
	var form m_sspslot.SspSlot
	if err := c.Ctx.ShouldBind(&form); err != nil {
		fmt.Println(err)
		c.BadRequest()
		return
	}
	utils.CleanStruct(&form)
	ssp_uid := comm.GetUserInfo(c)
	if ssp_uid == "" {
		return
	}
	form.SspUid = ssp_uid
	_, ok := cache.CacheSspAppMapNGet(form.Mid)
	if form.Mid == "" || !ok {
		c.SetErrorMsg(nil, errmsg.GetError("所属应用不存在"))
		return
	}
	slot_type_ck := utils.CkslotType(form.SlotType)
	if form.SlotType == "" || !slot_type_ck {
		c.SetErrorMsg(nil, errmsg.GetError("广告位类型选择错误"))
		return
	}
	form.Code = utils.RandomString(8)
	if form.SlotId == "" {
		form.SlotId = utils.RandomString(6)
	}
	if form.SlotName == "" {
		form.SlotName = form.Name
	}
	id, err := m_sspslot.Save(form)
	if err != nil {
		c.SetErrorMsg(nil, errmsg.GetError("insert is error"))
		return
	}
	go cache.CacheSspSlotMapUpdate(id)
	c.SetErrorMsg(nil, errmsg.SuccessMsg())
	return
}

func Edit(c *handlers.Controller) {
	var form m_sspslot.SspSlot
	if err := c.Ctx.ShouldBind(&form); err != nil {
		fmt.Println(err)
		c.BadRequest()
		return
	}
	utils.CleanStruct(&form)
	ssp_uid := comm.GetUserInfo(c)
	if ssp_uid == "" {
		return
	}
	sspinfo, err := m_sspslot.GetByCk(form.Id.Hex(), ssp_uid)
	if err != nil || sspinfo == nil {
		c.SetErrorMsg(nil, errmsg.GetError("data is empty"))
		return
	}
	err = m_sspslot.Update(form)
	if err != nil {
		c.SetErrorMsg(nil, errmsg.GetError("edit is error"))
		return
	}
	go cache.CacheSspSlotMapUpdate(sspinfo.Id.Hex())
	c.SetErrorMsg(nil, errmsg.SuccessMsg())
	return
}

func Info(c *handlers.Controller) {
	id := c.GetString("id")
	if id == "" {
		c.SetErrorMsg(nil, errmsg.GetError("必填参数为空"))
		return
	}
	ssp_uid := comm.GetUserInfo(c)
	if ssp_uid == "" {
		return
	}
	data, err := m_sspslot.GetByCk(id, ssp_uid)
	if err != nil {
		c.SetErrorMsg(nil, errmsg.GetError("select info is error"))
		return
	}
	c.SetErrorMsg(data, errmsg.SuccessMsg())
	return
}

func Flag(c *handlers.Controller) {
	id := c.GetString("id")
	flag := c.GetInt32("flag", 0)
	if id == "" || (flag != 1 && flag != 2) {
		c.SetErrorMsg(nil, errmsg.GetError("非法参数"))
		return
	}
	ssp_uid := comm.GetUserInfo(c)
	if ssp_uid == "" {
		return
	}
	data, err := m_sspslot.GetByCk(id, ssp_uid)
	if err != nil || data == nil {
		c.SetErrorMsg(nil, errmsg.GetError("select info is error"))
		return
	}
	err = m_sspslot.UpdateFalg(id, flag)
	if err != nil {
		c.SetErrorMsg(nil, errmsg.GetError("flag is error"))
		return
	}
	go cache.CacheSspSlotMapUpdate(id)
	c.SetErrorMsg(nil, errmsg.SuccessMsg())
	return
}

func Stats(c *handlers.Controller) {
	id := c.GetString("id")
	flag := c.GetInt32("status", 0)
	if id == "" || (flag != 1 && flag != 2) {
		c.SetErrorMsg(nil, errmsg.GetError("非法参数"))
		return
	}
	ssp_uid := comm.GetUserInfo(c)
	if ssp_uid == "" {
		return
	}
	data, err := m_sspslot.GetByCk(id, ssp_uid)
	if err != nil || data == nil {
		c.SetErrorMsg(nil, errmsg.GetError("select info is error"))
		return
	}
	err = m_sspslot.UpdateDel(id, flag)
	if err != nil {
		c.SetErrorMsg(nil, errmsg.GetError("stats is error"))
		return
	}
	go cache.CacheSspSlotMapUpdate(id)
	c.SetErrorMsg(nil, errmsg.SuccessMsg())
	return
}
